<?
$start = time();

if (strlen($rep_server)>0)
{ 
	$nntp_server   = $rep_server;  
	$nntp = new NNTP($nntp_server, $user, $pass); 											  
	if (!$nntp->connect()) 
	{ 
		header("Content-Error: " . $nntp->get_error_message());
		echo "<b>".$messages_ini["error"]["nntp_fail"]."</b><br>"; 
		echo $nntp->get_error_message().$nntp_server."<br>"; 
		exit;
	}  
}


$pp_reg=stripslashes($_POST["pp_reg"]);
$pp_cmd=$_POST["pp_cmd"];
$pp_bin=$_POST["pp_bin"];
$pp_grp=$_POST["pp_grp"];
$pp_safe=$_POST["pp_safe"];
$pp_param=$_POST["pp_param"];
$pp_log=$_POST["pp_log"];


if (strlen($pp_bin)==0) 
{ ?>
	<form action="<?=$href?>?<?= $qs ?>" method="post">
	   Command: <input name=pp_cmd value="<?= $pp_cmd ?>">
	   Param: <input name=pp_param value="<?= $pp_param ?>">
	   RegEx: <input name=pp_reg size=70 value="<?= $pp_reg ?>">
	   <br>
	   Max Results: <input name=pp_safe value="<?= $pp_safe ?>">
	   Log To: <input name=pp_log value="<?= $pp_log ?>">
	   Group: <input name=pp_grp value="<?= $pp_grp ?>">
	   <input type="checkbox" name=pp_bin value="1"> image output
	   <input type="submit" value="Send">
	</form><?= $pp_reg ?>
	
	<hr>
		<div id='dstat'></div>
		<div style='width:642px;height:10px;border:inset 1px;margin:2px;background-color:#FFFFFF;'
	  id='dprog'><img height='12' width='1' id='iprog' src='images/progress.gif'></div>	
	  
	
	<hr> <? 
}
else
{
	header ("Content-Type: image/jpeg");
}
 
 $mask = array ("all","prefix","is_erotic","title","count");
 $count = 0;
  
 
if (strlen($pp_grp)>0)
{
	$start = time();
	
	if ( $gi=$nntp->join_group(trim($pp_grp)) )
	{
		# print_r ($gi);
		$start_id = $gi["start_id"];
		$end_id   = $gi["end_id"];
		 
$end = time() - $start; 
print "1- " . $end . "<br>";


		$list_max_count = $end_id - $start_id; 
		print $list_max_count . " articles.<br>"; 
		$twenty_percent = max ($pp_safe, floor($list_max_count * .05)); 
		
		if ($twenty_percent > $list_max_count)
		{
		    $twenty_percent = $list_max_count;
		}
		if ($twenty_percent > $pp_safe)
		{
		    $twenty_percent = $pp_safe;
		}
		$start_id = $end_id - $twenty_percent; 
		print "last " . $twenty_percent . " articles.<br>";
		   
		$buf = $nntp->send_request("xover " . $start_id . "-" . $end_id);    
		$response = $nntp->parse_response($buf); 
		 
$end = time() - $start; 
print "2- " . $end . "<br>";
  
		print "<xmp>[";
		print $response["message"];
		print "]</xmp>";
		
		$i=0;
		$articles = array();
		$arr = ""; 
			
                     
	if ($response["status"] == ARTICLE_OVERVIEW) 
	{ 
		$buf = fgets($nntp->nntp, 4096); 
		
$end = time() - $start; 
print "3- " . $end . "<br>";

		while (!preg_match("/^\.\s*$/", $buf)) 
		{  
			$elements = preg_split("/\t/", $buf);  
			$i++; 
			raise_event (TEST_PAGE_PROGRESS, $i, $twenty_percent, $start);
			
			if (trim($buf)==".")
			{ 
				break;
			}
			
			$subject     = $elements [1];
			
			$mf = multifile_lookup ($subject, $nntp, true);
			if ($mf["count"]>1)
			{
			    $subject = $mf["root"];
			#	print $subject . "<br>";
			}
			
			$message_id  = $elements [0];  
			$pattern     = preg_quote($subject);  
			$pattern     = preg_replace (array ("/(\d+)/", "/\//"), array("(\d+)", "\\/"), $pattern);  
			
			if (trim($pattern)!="")
			{
				$pattern     = "/". trim($pattern) . "/i"; 
				if ( array_key_exists($pattern, $patterns)) 
				{
					$patterns[$pattern][] = $elements;
				}
				else
				{
					$patterns[$pattern] = array($elements); 
				}   
			}
		    $buf = fgets($nntp->nntp, 4096); 
		}
	}
	
$end = time() - $start; 
print "4- " . $end . "<br>";
    
	// Compare function for sorting message by date in ascending order
	function pat_by_subj($msg1, $msg2) {
		if ($msg1[0][1] == $msg2[0][1]) {
			return 0;
		}
		return ($msg1[0][1] < $msg2[0][1])?-1:1;
	}
	reset ($patterns);
	
			uasort($patterns, pat_by_subj);
	
	
	print count ($patterns) . " unique articles<hr noshade><table>";
	 $preview = 0;
     while (list ($regex_expression, $matching_articles) = each ($patterns))  
	{ #http://www.101stratford.com/nntp/skin.php?nntp=news-west.usenet.com&a=slide&message_id=163316729&art_group=alt.binaries.pictures.erotica.breasts
	   
	   $elements = $matching_articles[0];
	   $link = "nntp=" . $nntp_server . "&message_id=" . $elements[0] . "&art_group=" . trim($pp_grp);
	   
	   $ok = false;
	   $view = "";
	   $id   = "";
	   $hgt  = "";
	   
	   if (preg_match ("/(.*)\.(jpg|gif|bmp|png)/i", $elements[1], $tmp))
	   {
		   $preview ++;
		   if ($preview>10)
		   {
		       $view = "<a href='".$href."?".$link."' target=_blank>View</a>";
		   }
		   else
		   {
	           $view = strpos(strtolower($elements[1]), "re:")===false ? $href."?".$link : "";
		       $id   = "apreload";
			   $hgt  = "width:120;height:180;";
		   }
		   
		   
	   } 
	   $subject = clean_subject($elements[1]); 
	   print "<tr>"; //<td colspan=8>".$regex_expression."</td></tr><tr>";
	   
	   print "<td align=center><div style='" . $hgt . "overflow:hidden' id='".$id."'>".$view."</div></td>";
	   print "<td><a href='".$href."?".$link."&a=slide' target=_blank>Slideshow</a></td>";
	   print "<td><a href='nzb.php?".$link."&a=gnzb' target=_blank>NZB</a></td>";
	   print "<td nowrap>" . $elements[3] . "</td>";
	   print "<td nowrap>" . $elements[6] . "</td>";
	   print "<td nowrap>" . $elements[7] . "</td>";
	   print "<td>" .$subject. "</td>";
	   print "<td>" . count($matching_articles) . "</td>";
	   print "</tr>";
	}
	print "</table>";
	
$end = time() - $start; 
print "5- " . $end . "<br>"; 

?>
<script language="javascript">
var p=0;
function preload (a,b)
{
    var d=document.all ("apreload");
    if (a)
	{
	   d[b].innerHTML = "Image not available";
	}
    if (p<d.length)
	{
		d[p].innerHTML = "<img onload='preload(false)' onerror='preload(true,"+p+")' src='"+d[p].innerHTML+"&pp_w=120'>";
		p++;
	}
}
preload ()
</script>


<?
print "Done " . $safe . " lines ";
$nntp->quit();
		 
		 exit;
	 }
}

 
if (strlen($pp_cmd)>0)
{
	if (strlen($pp_bin)==0) 
	{
		print "Sending...".$pp_cmd." ".$pp_param."<br>";
	}
	$buf = $nntp->send_request($pp_cmd." ".$pp_param);
	$response = $nntp->parse_response($buf); 
	$header = ""; 
	if (strlen($pp_bin)==0) 
	{
		print "[<xmp>".$buf."</xmp>]";
		print "[<xmp>";
		print_r ($response);
		print "</xmp>]";
	}
	
	$buf = fgets($nntp->nntp, 4096); 
	if (strlen($pp_bin)==0) 
	{
		print "[<xmp>".$buf."</xmp>]";
	}
	
	if (strlen($pp_log)>0)
	{
		$handle=fopen($pp_log,'w');
	}
	$safe=0;
	$now=time();
	while (strlen($buf) > 0) { 
		$elements = preg_split("/\s/", $buf); 
		$header .= $buf; 
		
		if ($safe>$pp_safe&&pp_safe!="")
		{
			print "<font color=red>".$safe." records is too many!</font>";
			break;
		}
		
		if (trim($buf)==".")
		{ 
			break;
		}
		
		
		$date_stamp = strtotime($elements[3]); 
		$since=$now-$date_stamp;
		 
		if (strlen($pp_log)>0)
		{   
			if ($since<(7*86400))
			{
				$safe++;
				fputs($handle,$buf);
			}
		}
		else if (strlen($pp_bin)==0&&strlen($pp_reg)==0) 
		{
			
			$safe++;
			print "<xmp>".$buf."</xmp>"; 
		}
		else
		{
			 #$enc  = new yenc(); 
			 #print $enc->decode($buf);  
		}
		
		$buf = fgets($nntp->nntp, 4096); 
		if ($pp_reg!=""&&strlen(trim($buf))>0&&strlen($pp_bin)==0)
		{
		    $safe++;
			if ( preg_match($pp_reg, $buf, $bf) )
			{
			    $count ++;
				if ($bf[1]-$bf[2]>1000000)
				{
					print "[<xmp>".$buf."</xmp>]";
				 #    print_r ($bf); 
					 print "(".($bf[1]-$bf[2]).")";
				}
			}
		}
	}     
	
	if (strlen($pp_log)>0)
	{
		fclose($handle);
		print "<xmp>".$buf."</xmp>";
		print "<br><a href='".$pp_log."' target=_blank>".$pp_log." (".filesize($pp_log)." bytes)</a>";
	}
}  
print "Done " . $safe . " lines ";
$nntp->quit();
exit;
?>